package com.eece496.Server;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eece496.Database.Grades;
import com.eece496.Database.Student;
import com.eece496.Database.StudentGroups;
import com.eece496.Database.TAEnrollment;
import com.eece496.Helpers.CourseInfo;
import com.eece496.Helpers.GroupInfo;
import com.eece496.Helpers.StudentGradeInfo;

/**
 * Servlet implementation class StudentViewGradesServlet
 */
public class StudentViewGradesServlet extends MasterServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public StudentViewGradesServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Connect();

		Grades GradesDB = new Grades();
		GradesDB.Setup(connection);

		// Get data from jsp
		String ButtonClicked = request.getParameter("button");
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String sID = request.getParameter("studentID");
		System.out.println(sID);
		String String1 = request.getParameter("courseNumber");
		String courseNumber = String1.substring(0, 8);
		String sectionNumber = String1.substring(9, 12);
		System.out.println(courseNumber);
		System.out.println(sectionNumber);
		String OverallIndGrade = null;
		String OverallGroupGrade = null;
		String TotalGrade = null;

		int studentID = Integer.parseInt(sID);

		//If student selected to View their grades
		if (ButtonClicked.equals("View Grades")) {

			String isEmpty = "false";
			ArrayList<StudentGradeInfo> info = new ArrayList<StudentGradeInfo>();
			
			ArrayList<String> IndGrades = new ArrayList<String>();
			ArrayList<String> GroupGrades = new ArrayList<String>();
			
			try {
				info = GradesDB.GetAllGradesFromStudent(studentID,
						courseNumber, sectionNumber);
				IndGrades = GradesDB.GetGradeType(courseNumber, sectionNumber, studentID, "individual");
				GroupGrades = GradesDB.GetGradeType(courseNumber, sectionNumber, studentID, "group");
				
				System.out.println("Ind Grades: " + IndGrades.size());
				System.out.println("Group Grades: " + GroupGrades.size());
				/*
				//Compute IndGrade
				if(IndGrades.size() == 0)
				{
					OverallIndGrade ="Not Available";
				}
				else
				{
					int OverallGrade = 0;
					int size = 0;
					size = IndGrades.size();
					for(String str: IndGrades)
					{
						
						if(str.equals("NA") || str.equals("AB"))
						{
							size--;
							OverallIndGrade = "NA";
							continue;
						}
						OverallGrade = ((OverallGrade + Integer.parseInt(str)) / size) * 100;
						OverallIndGrade = Integer.toString(OverallGrade);
						
					}
				}
				
				//Compute the GroupGrade
				if(GroupGrades.size() == 0)
				{
					OverallGroupGrade ="Not Available";
				}
				else
				{
					int OverallGrade = 0;
					int size = 0;
					size = GroupGrades.size();
					for(String str: GroupGrades)
					{
						if(str.equals("NA") || str.equals("AB"))
						{
							size--;
							OverallGroupGrade = "NA";
							continue;
						}
						System.out.println(str);
						OverallGrade = ((OverallGrade + Integer.parseInt(str)) / size) * 10;
						OverallGroupGrade = Integer.toString(OverallGrade);
						
					}
				}
				
				//Computer Overall Grade
				if(!GroupGrades.isEmpty() && !IndGrades.isEmpty())
				{
					if(OverallGroupGrade.equals("NA") || OverallIndGrade.equals("NA"))
					{
						TotalGrade ="NA";
					}
					else{
					int grade = (Integer.parseInt(OverallGroupGrade) + Integer.parseInt(OverallIndGrade)) /2;
					TotalGrade = Integer.toString(grade);
					}
				}
				else
					TotalGrade ="Not Available";
				
				*/
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			System.out.println(info.size());
			request.setAttribute("studentID", sID);
			request.setAttribute("firstname", firstname);
			request.setAttribute("lastname", lastname);
			request.setAttribute("GradeInfo", info);
			request.setAttribute("isEmpty", isEmpty);
			request.setAttribute("courseNumber", courseNumber);
			request.setAttribute("sectionNumber", sectionNumber);
			request.setAttribute("OverallIndGrade",OverallIndGrade );
			request.setAttribute("OverallGroupGrade",OverallGroupGrade );
			request.setAttribute("OverallGrade", TotalGrade);
			
			if (info.size() == 0) {
				isEmpty.equals("true");
				System.out.println(isEmpty);
			}

			request.getRequestDispatcher("student/student-grades.jsp").forward(
					request, response);
		}
		
		//If student select to View Their group and team members
		else if(ButtonClicked.equals("View Group")){
			//get your groupNumber
			StudentGroups GroupDB = new StudentGroups();
			int groupNumber =0;
			String division = null;
			ArrayList<GroupInfo> groupList = new ArrayList<GroupInfo>();
			String gNum = null;
			
			GroupDB.Setup(connection);
			try {
				groupNumber = GroupDB.GetGroupNumberFromStudent(studentID, courseNumber, sectionNumber);
				division = GroupDB.GetDivisionFromStudent(studentID, courseNumber, sectionNumber);
				gNum = Integer.toString(groupNumber);
				groupList = GroupDB.GetStudentGroupInfo(courseNumber, sectionNumber, groupNumber, division);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			request.setAttribute("division", division);
			request.setAttribute("studentID", sID);
			request.setAttribute("groupNumber", gNum);
			request.setAttribute("groupList", groupList);
			request.setAttribute("firstname", firstname);
			request.setAttribute("lastname", lastname);
			request.setAttribute("courseNumber", courseNumber);
			request.setAttribute("sectionNumber", sectionNumber);
			
			request.getRequestDispatcher("student/student-group.jsp").forward(
					request, response);
		}
	}

}
